package com.yidu.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yidu.comment.Tools;
import com.yidu.dao.SnMaDetailMapper;
import com.yidu.dao.SnMaInDetailMapper;
import com.yidu.dao.SnMaInventoryMapper;
import com.yidu.dao.SnMaPurchaseMapper;
import com.yidu.dao.SnMaQualityMapper;
import com.yidu.dao.SnMaterialsMapper;
import com.yidu.model.SnComInDetail;
import com.yidu.model.SnComInventory;
import com.yidu.model.SnCommodity;
import com.yidu.model.SnMaDetail;
import com.yidu.model.SnMaInDetail;
import com.yidu.model.SnMaInventory;
import com.yidu.model.SnMaPurchase;
import com.yidu.model.SnMaQuality;
import com.yidu.model.SnMaterials;
import com.yidu.model.SnProDetail;
import com.yidu.model.SnProduction;
import com.yidu.model.SnQuDetail;
import com.yidu.model.SnQuality;
import com.yidu.service.SnMaQuDetailService;

/**
 * 材料采购质检实现类
 * @author chensituo
 * @date 2018年12月17日 上午12:00:22
 */
@Service
public class SnMaQuDetailServiceImpl implements SnMaQuDetailService{
	/**
	 * 材料采购质检详情mapper
	 */
	@Resource
	SnMaDetailMapper maDetailMapper;

	/**
	 * 材料采购质检mapper
	 */
	@Resource
	SnMaQualityMapper maqualityMapper;
	/**
	 * 注入材料采购mapper
	 */
	@Resource
	SnMaPurchaseMapper mapurchaseMapper;
	/**
	 * 注入原材料mapper
	 */
	@Resource
	SnMaterialsMapper materialisMapper;
	/**
	 * 注入材料库存mapper
	 */
	@Resource
	SnMaInventoryMapper mainventoryMapper;
	/**
	 * 注入材料库存记录mapper
	 */
	@Resource
	SnMaInDetailMapper mainDetailMapper;
	@Override
	public List<SnProDetail> findAll(Map<String, Object> maps) {
		//调用查询所有的方法
		return maDetailMapper.findAll(maps);
	}

	@Override
	public int findCount(Map<String, Object> maps) {
		//调用查询行数的方法
		return maDetailMapper.findCount(maps);
	}

	@Override
	public int updateNotCount(String sum) {
		//将字符串根据@拆分
		String [] strings=sum.split("@");
		int rows=0;
		String mqId="";
		int aleady=0;
		int notcount=0;
		//遍历
		for (String string : strings) {
			//根据，拆分
			String [] arr= string.split(",");
			//根据生产质检详情id查询的到对象
			SnMaDetail maDetail=maDetailMapper.selectByPrimaryKey(arr[0]);
			//将对象值修改
			maDetail.setMdNotcount(Integer.valueOf(arr[1]));
			//得到所有没通过的数量
			notcount+=Integer.valueOf(arr[1]);
			maDetail.setMdAlready(maDetail.getMdCount()-maDetail.getMdNotcount());
			//得到所有通过的数量
			aleady+=maDetail.getMdAlready();
			maDetail.setMdProbability(Math.round(((Double.valueOf(maDetail.getMdAlready())/Double.valueOf(maDetail.getMdCount()))*100))+"%");
			rows=maDetailMapper.updateByPrimaryKeySelective(maDetail);
			//将材料采购质检id赋值
			mqId=maDetail.getMqId();
		}
		//根据质检id查询
		SnMaQuality quality=maqualityMapper.selectByPrimaryKey(mqId);
		//将状态修改为1
		quality.setOper("1");
		quality.setMqAlready(aleady);
		quality.setMqNotcount(notcount);
		quality.setMqProbability(((Double.valueOf(quality.getMqAlready())/Double.valueOf(quality.getMqCount()))*100)+"%");
		int row=maqualityMapper.updateByPrimaryKeySelective(quality);
		//根据材料采购id插叙
		SnMaPurchase maPurchase=mapurchaseMapper.selectByPrimaryKey(quality.getPurId());
		//将状态修改为6
		maPurchase.setOper("6");
		int rowsa=mapurchaseMapper.updateByPrimaryKeySelective(maPurchase);
		//如果都成功返回1
		if(rows==1 && row==1 && rowsa==1) {
			return 1;
		}else {
			return 0;
		}
	}

	@Override
	public int updateSh(String mqId) {
		//根据质检id查询
		SnMaQuality quality=maqualityMapper.selectByPrimaryKey(mqId);
		//将状态修改为3
		quality.setOper("3");
		int ro=maqualityMapper.updateByPrimaryKeySelective(quality);
		//根据质检id查询材料id和质检数量
		List<Map<String, Object>> inte=maqualityMapper.selectComId(mqId);
		//去重复
		for  ( int  i  =   0 ; i  <  inte.size()  -   1 ; i ++ )  {       
			for  ( int  j  =  inte.size()  -   1 ; j  >  i; j -- )  {       
				if  (inte.get(j).get("md_id").equals(inte.get(i).get("md_id")))  {       
					inte.remove(j);       
				}        
			}        
		}  
		int rows=0;
		int row=0;
		for (Map<String, Object> map : inte) {
			
			//根据材料id查询材料
			SnMaterials materials=materialisMapper.selectByPrimaryKey((String) map.get("ma_id"));
			//增加商品库存
			System.err.println(materials.getMaName()+"----------------------"+map.get("md_already"));
			//根据材料id查询 材料的库存
			SnMaInventory maInventory=mainventoryMapper.selectMaBraId(materials.getMaId());
			System.err.println("************"+materials.getMaId());
			if(maInventory!=null && !"".equals(maInventory)) {
				//修改材料库存数量
				maInventory.setInNumber(maInventory.getInNumber().intValue()+Integer.valueOf(map.get("md_already").toString()));
				//调用修改的方法
				rows=mainventoryMapper.updateByPrimaryKeySelective(maInventory);

				//创建材料库存记录对象
				SnMaInDetail maInDetail=new SnMaInDetail();
				//赋值
				maInDetail.setSort(Integer.valueOf(Tools.getTimeStamp()));
				maInDetail.setInId(maInventory.getInId());
				maInDetail.setMidId(Tools.getDateOrderNo());
				maInDetail.setMidExplain("采购"+materials.getMaName()+"增加"+materials.getMaName()+"库存"+map.get("md_already")+materials.getMaUnit());
				maInDetail.setOptime(Tools.getCurDateTime());
				maInDetail.setIsva(1);
				//调用增加材料库存记录的方法
				row=mainDetailMapper.insertSelective(maInDetail);
			}
		}
		if(ro==1 &&  rows==1 && row==1) {
			return 1;
		}else {
			return 0;
		}

	}
}
